package com.qishon.pc.domain.util;

import com.qishon.pc.domain.model.ProgressControl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
/**
 * Created by yuquan.hu on 2017/1/5.
 */
@Slf4j
@Component


public class UploadDatabase {
    public List<String> part = new ArrayList<String>();

    @Value("${database.AccessTokenPath}")
    private String AccessTokenPath;

    @Value("${database.UpLoadPath}")
    private String UpLoadPath;

    public void FileUpload(String path,String Uppath,String modelName) throws IOException{
        String clientId = "webTestKey";
        Long time = System.currentTimeMillis();
        String requestTime = time.toString();
        String secret = "webTestSecret";
        String chksum = EncryptionMd5(clientId + requestTime + secret);
        Map<String, String> param = new HashMap<String, String>();
        param.put("clientId", clientId);
        param.put("requestTime", requestTime);
        param.put("chksum", chksum);
        HttpRequestUtils token = new HttpRequestUtils();
        HttpResult result = token.sendByPost(AccessTokenPath, param);
        String AccessToken = GetAccessToken(result.getResult());
        File f = new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        GetName(f);
        GetJosn(AccessToken,Uppath,modelName);
    }
    //MD5加密
    public String EncryptionMd5(String chksum){
        try{
            MessageDigest md;
            md = MessageDigest.getInstance("MD5");
            md.update(chksum.getBytes());
            return new BigInteger(1,md.digest()).toString(16);
        }catch(NoSuchAlgorithmException e){
            e.printStackTrace();
        }
        return null;
    }
    public String GetAccessToken(String result){
        JSONObject rtn = JSONObject.fromObject(result);
        return rtn.getJSONObject("result").getJSONObject("info").get("accessToken").toString();
    }
    public void GetJosn(String re,String Uppath,String girardName) throws IOException{
        String filepath = Uppath;
        for(int i = 0;i < part.size();i++){
            Map<String, String> Json = new HashMap<String, String>();
            Json.put("interfaceCode", "SALE_WEB_UPLOAD");
            Json.put("access_token",re);
            Json.put("usrKey", "1");
            Json.put("companyKey", "12");
            Json.put("prodCode", girardName);
            Json.put("regionCode", part.get(i));
            filepath += "/";
            filepath += part.get(i);
            i++;
            Json.put("partCode",  part.get(i));
            filepath += "/";
            filepath += part.get(i);
            i++;
            Json.put("fabricCode",  part.get(i));
            filepath += "/";
            filepath += part.get(i);
            i++;
            filepath += "/";
            filepath += part.get(i);
            Json.put("pictPath", filepath);

            Map<String,String> shaplist = new HashMap<String, String>();
            shaplist.put("sourceFileName",  part.get(i));
            shaplist.put("fileShapCode", "S1");
            JSONObject list = JSONObject.fromObject(shaplist);

            Json.put("shapList", "[" + list.toString() + "]");
            Json.put("arrayJsonKey", "shapList");
            JSONObject a = JSONObject.fromObject(Json);
    //        System.out.println(a);
            HttpRequestUtils token1 = new HttpRequestUtils();
            HttpResult result = token1.sendByPost(UpLoadPath,Json);
      //      System.out.println(result.getResult());
        }
    }
    public void GetName(File path){
        boolean iseffective;
        File[] files =path.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                GetName(file);
            }
            else {
                String regex = "([0-9]|[a-z]|[A-Z]|_)*_Full.png";
                iseffective = file.getName().matches(regex);
                if(iseffective){
                    String Parentpath = file.getParentFile().toString();
                    String division[] = Parentpath.split("\\\\");
                    for(int j = division.length - 3;j < division.length;j++){
                        part.add(division[j]);
                    }
                    part.add(file.getName());
                }
            }
        }
  //      for(int i = 0;i < part.size();i++){
   //         System.out.println(part.get(i));
  //      }
    }
}
